<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="../d3-tip.css" />
    <style>
      ul,li{
        margin:0 ;
        padding: 0;
      }
      button{
        margin: 10px;
      }
    </style>
  </head>
  <body>
    <ul>
      <li><button id="x-plus">x+</button><button id="x-minus" >x-</button></li>
      <li><button id="y-plus">y+</button><button id="y-minus">y-</button></li>
      <li><button id="z-plus">z+</button><button id="z-minus">z-</button></li>
    </ul>

    <script src="../d3.js"></script>
    <script src="../d3-v6-tip.js"></script>
    <script src="../topojson.min.js"></script>
    <script>

      const width = 800 ;
      const height = 600 ;
      const svg = d3
        .select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", height)
        .style("border", "1px solid #ccc");

      (async ()=>{
        
        let data = await d3.json('data/countries.json');
        console.log(data);
        data = topojson.feature(data,data.objects.countries) ;
        console.log(data);

        const projection = d3.geoAzimuthalEqualArea()
        //const projection = d3.geoNaturalEarth1()
          .fitExtent([[50,50],[width-50,height-50]] , data)

        const path = d3.geoPath(projection);

        const tip = d3.tip()
          .attr('class','d3-tip')
          .html((e,d)=>d.properties.name)
        tip(svg);  

        
        const g = svg.append('g');
        const g2 = svg.append('g');
        const graticule = d3.geoGraticule() 

        function draw(){
          //绘制地图
          g.selectAll('path')
          .data(data.features)
          .join('path')
          .attr('d',path)
          .attr('fill',(d,i)=>d3.schemeSet3[i%d3.schemeSet3.length])
          .attr('stroke','#ccc')
          .on('mouseover',(e,d)=>{
            d3.select(e.target)
              .attr('stroke-width',2)
              .attr('opacity',0.6)
            tip.show(e,d)  
          })
          .on('mouseout',function(e,d){
            d3.select(this)
              .attr('stroke-width',1)
              .attr('opacity',1)
            tip.hide(e,d)  
          })
        

          //绘制网格线  
          g2
            .selectAll('path')
            .data([graticule()])
            .join('path')
            .attr('d',path)
            .attr('fill','none')
            .attr('stroke','#ccc')
        }
        draw();
        



        let xAngle=0, yAngle=0 , zAngle=0 ;
         //事件旋转
         document.querySelector('#x-plus').onclick = function(){
            xAngle += 10 ;
            rotate();
         }
         document.querySelector('#y-plus').onclick = function(){
            yAngle += 10 ;
            rotate();
         }
         document.querySelector('#z-plus').onclick = function(){
            zAngle += 10 ;
            rotate();
         }
         document.querySelector('#x-minus').onclick = function(){
            xAngle -= 10 ;
            rotate();
         }
         document.querySelector('#y-minus').onclick = function(){
            yAngle -= 10 ;
            rotate();
         }
         document.querySelector('#z-minus').onclick = function(){
            zAngle -= 10 ;
            rotate();
         }
         
         
         function rotate(){
            projection.rotate([xAngle,yAngle,zAngle]);
            draw();
         }
      })()
    </script>
  </body>
</html>
